package org.itboys.core.service.mysql.web;

import org.itboys.commons.utils.concurrent.DelayTaskQueue;
import org.itboys.commons.utils.concurrent.Task;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.order.SundayPriceAdjustOrderMapper;
import org.itboys.core.dao.order.SundayPriceAdjustSKUMapper;
import org.itboys.core.entity.mongo.SundayPriceAdjustSKU;
import org.itboys.core.entity.mysql.SundayPriceAdjustOrder;
import org.itboys.core.entity.mysql.SundaySkuPrice;
import org.itboys.mysql.service.BaseMySqlService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author：贾琦磊
 * @Date：2018/9/3
 * @Description：价格调整单，状态审核过后不可更改，同时生成价格调整任务
 */
@Service
public class SundayPriceAdjustOrderService extends BaseMySqlService<SundayPriceAdjustOrder>implements InitializingBean {
    @Autowired
    private SundayPriceAdjustOrderMapper priceAdjustOrderMapper;
    @Autowired
    private SundayPriceAdjustSKUMapper priceAdjustSKUMapper;
    @Autowired
    private SundaySkuPriceService skuPriceService;

    /**
     * 调整单保存和修改
     * @param id
     * @param priceAdjustOrder
     * @param priceAdjustSKUS
     */
    public void save(Long id, SundayPriceAdjustOrder priceAdjustOrder, List<SundayPriceAdjustSKU> priceAdjustSKUS){
        Long orderId;
        if(LongUtil.isNotZreo(id)){
            orderId=id;
            priceAdjustOrder.setId(id);
            priceAdjustOrderMapper.update(priceAdjustOrder);
        }else{
            priceAdjustOrderMapper.insert(priceAdjustOrder);
            orderId=priceAdjustOrder.getId();
        }
        Map<String,Object> param=new HashMap<>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("orderId",orderId);
        priceAdjustSKUMapper.deleteByOrderId(orderId);
        for (SundayPriceAdjustSKU s:priceAdjustSKUS
             ) {
            s.setOrderId(orderId);
        }
        priceAdjustSKUMapper.batchSave(priceAdjustSKUS);
    }



    @Override
    public void afterPropertiesSet() throws Exception {

    }
}
